import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/http/api.dart';
import 'package:yuan_hang_app/http/http_utils.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/views/report/model/core_rate_report.dart';

import '../../res/w_colors.dart';
import '../../res/w_dimens.dart';
import '../../widget/label_item.dart';
import '../../widget/status_btn.dart';
import '../../widget/w_text.dart';

class CoreReportDetailPage extends StatefulWidget {
  final String? dealerCode;
  final String? atYear;
  final String? atMonth;

  const CoreReportDetailPage(
      {super.key, this.dealerCode, this.atYear, this.atMonth});

  @override
  State<StatefulWidget> createState() => CoreReportDetailPageState();
}

class CoreReportDetailPageState extends BaseState<CoreReportDetailPage> {
  List<String> titleList = [
    "总\n线\n索",
    "有\n效\n线\n索",
    "到\n店",
    "试\n乘\n试\n驾",
    "成\n交"
  ];
  List<List<String>> dataTitleList = [
    ["总线索目标", "总线索量", "缺口", "总线索目标完成率", "总线索目标完成率目标"],
    ["有效线索目标", "有效线索量", "缺口", "线索有效率", "线索有效率目标"],
    ["当月实际到店目标", "当月实际到店量", "缺口", "有效线索到店率", "有效线索到店率目标"],
    ["试驾目标", "试驾量", "缺口", "到店试驾率", "到店试驾率目标"],
    ["销量目标", "销量", "缺口", "到店成交率", "到店成交率目标"]
  ];

  Map<String, dynamic> queryParams = {};

  List<List<String?>> dataList = [];

  CoreRateReport? reportObject;

  @override
  void initState() {
    super.initState();
    queryParams["dealerCode"] = widget.dealerCode;
    queryParams["atYear"] = widget.atYear;
    queryParams["atMonth"] = widget.atMonth;
    fetchReportDetail();
  }

  void fetchReportDetail() {
    showLoading();
    HttpUtils().get(Api.getCoreReportDetail, queryParams: queryParams).then((value) {
      hideLoading();
      if (value.resultCode == 200) {
        reportObject = CoreRateReport.fromJson(value.data[0]);
        dataList = [
          [
            reportObject?.clueNumT!.toString(),
            reportObject?.clueNum!.toString(),
            reportObject?.clueGap!.toString(),
            reportObject?.clueRate!.toString(),
            reportObject?.clueTargetRate!.toString()
          ],
          [
            reportObject?.validNumT!.toString(),
            reportObject?.validNum!.toString(),
            reportObject?.validGap!.toString(),
            reportObject?.validRate!.toString(),
            reportObject?.validTargetRate!.toString()
          ],
          [
            reportObject?.inshopNumT!.toString(),
            reportObject?.inshopNum!.toString(),
            reportObject?.inshopGap!.toString(),
            reportObject?.inshopRate!.toString(),
            reportObject?.inshopTargetRate!.toString()
          ],
          [
            reportObject?.testDriveNumT!.toString(),
            reportObject?.testDriveNum!.toString(),
            reportObject?.testDriveGap!.toString(),
            reportObject?.testDriveRate!.toString(),
            reportObject?.testDriveTargetRate!.toString()
          ],
          [
            reportObject?.orderNumT!.toString(),
            reportObject?.saleNum!.toString(),
            reportObject?.saleGap!.toString(),
            reportObject?.saleRate!.toString(),
            reportObject?.saleTargetRate!.toString()
          ],
        ];
        setState(() {});
      }
    });
  }

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "核心四率报表明细",
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            LabelItem(label: "直营店简称", value: reportObject?.dealerName),
            LabelItem(
                label: "统计时间", value: "${widget.atYear}-${widget.atMonth}"),
            LabelItem(label: "省份", value: reportObject?.provinceName),
            LabelItem(label: "城市", value: reportObject?.cityName),
            LabelItem(
                label: "销量目标完成率",
                value: reportObject?.saleTargetRate.toString()),
            _dataItem(titleList[0], dataTitleList[0], dataList.isEmpty ? [] : dataList[0]),
            _dataItem(titleList[1], dataTitleList[1], dataList.isEmpty ? [] : dataList[1]),
            _dataItem(titleList[2], dataTitleList[2], dataList.isEmpty ? [] : dataList[2]),
            _dataItem(titleList[3], dataTitleList[3], dataList.isEmpty ? [] : dataList[3]),
            _dataItem(titleList[4], dataTitleList[4], dataList.isEmpty ? [] : dataList[4]),
            SizedBox(
              height: 20.px,
            )
          ],
        ));
  }

  Widget _dataItem(title, dataTitles, List data) {
    return Container(
      decoration: BoxDecoration(boxShadow: [
        BoxShadow(
            color: WColors.CDFEFFB,
            offset: const Offset(0, 1),
            blurRadius: 3.px)
      ], borderRadius: BorderRadius.circular(10.px), color: Colors.white),
      margin: EdgeInsets.all(5.px),
      padding: EdgeInsets.only(top: 5.px, bottom: 5.px, right: 20.px),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          StatusBtn(
            fontSize: WDimens.tsMedium,
            height: 70.px,
            width: 20.px,
            text: title,
            color: WColors.primaryColor,
            isTag: true,
            radius: 0,
            fontColor: WColors.mainTextColor,
          ),
          SizedBox(
            width: 50.px,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _dataItemRow(dataTitles[0], data.isEmpty ? "" : data[0]),
                _dataItemRow(dataTitles[1], data.isEmpty ? "" : data[1]),
                _dataItemRow(dataTitles[2], data.isEmpty ? "" : data[2]),
                _dataItemRow(dataTitles[3], data.isEmpty ? "" : data[3]),
                _dataItemRow(dataTitles[4], data.isEmpty ? "" : data[4]),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _dataItemRow(dataTitle, data) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        WText(
          dataTitle,
          style: TextStyle(
              fontSize: WDimens.tsLabel, color: WColors.mainTextColor),
        ),
        WText(
          data,
          style: TextStyle(
              fontSize: WDimens.tsLabel, color: WColors.mainTextColor),
        ),
      ],
    );
  }
}
